<?php
	namespace Admin\Model;
	class GoodsModel extends \Think\Model{
		
		protected $_validate = array(
			array('name','require','用户名不能为空'),
			array('price','require','单价不能为空')

		);

		protected $_auto = array(
			array('addtime','time',1,'function')

			);
		public function getAll($limit){

			$list=$this->field('g.id         gid,g.name       gname, g.is_new     gnew,g.is_hot     ghot,g.is_bast    gb,g.price      gp, g.stok       gk,gi.name      giname,c.name       cname,g.addtime    gtime')->table('goods g,goods_img gi,category c')->where('g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1')->limit($limit)->select();

			return $list;
		}



		public function wcount($like){
			$li='';
			$li=$like;
			if (empty($li)) {
				$list=$this->field('g.id         gid,g.name       gname, g.is_new     gnew,g.is_hot     ghot,g.is_bast    gb,g.price      gp, g.stok       gk,gi.name      giname,c.name       cname,g.addtime    gtime')->table('goods g,goods_img gi,category c')->where('g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1')->count();
			}else
			{
				$list=$this->field('g.id         gid,g.name       gname, g.is_new     gnew,g.is_hot     ghot,g.is_bast    gb,g.price      gp, g.stok       gk,gi.name      giname,c.name       cname,g.addtime    gtime')->table('goods g,goods_img gi,category c')->where("g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1 and g.name like '%".$li."%'")->count();
			}
			
			return $list;

		}

		public function setadd($data,$filename){
			$this->startTrans();
			$id=$this->add($data);
				$img_moel=M('goodsImg');
				$dat['goods_id']=$id;
				$dat['is_face']=1;
				$dat['name']=$filename;

				$res=$img_moel->data($dat)->add();
				if($res){
					$this->commit();
					return true;
				}else{
					$this->rollback();
					return false;
				}

		}
		public function showadd(){
			$model = M('category');
			$sql="select id,name,pid,path,concat(path,id) bpath from category order by bpath";
			$list = $model->query($sql);
			foreach ($list as $k=> $v) {
				$num=count(explode(',',rtrim($v['path'],',')))-1;
				$list[$k]['name']=str_repeat('&nbsp;&nbsp;',$num).$v['name'];
			}
			return $list;

		}
		public function getlike($like,$limit){
			if(empty($like)){
				$list=$this->field('g.id         gid,g.name       gname, g.is_new     gnew,g.is_hot     ghot,g.is_bast    gb,g.price      gp, g.stok       gk,gi.name      giname,c.name       cname,g.addtime    gtime')->table('goods g,goods_img gi,category c')->where('g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1')->limit($limit)->select();
			}else{
				$list=$this->field('g.id gid,g.name   gname, g.is_new    gnew,g.is_hot ghot,g.is_bast gb,g.price      gp, g.stok  gk,gi.name  giname,c.name cname,g.addtime gtime')->table('goods g,goods_img gi,category c')->where("g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1 and g.name like '%".$like."%'")->limit($limit)->select();
				}
			return $list;
		}
			public function change($k,$status,$id){
			if($status){
				$data["$k"]=0;
			}else
			{
				$data["$k"]=1;
			}
			
	
			$this->where("id=$id")->save($data);
		}
			public function setmore($id){
				$list=$this->field('g.id gid,g.name  gname, g.stok gk,gi.name  giname,c.name  cname,g.addtime  gtime, gi.is_face gif,g.describe db')->table('goods g,goods_img gi,category c')->where('g.id=gi.goods_id and c.id=g.cate_id and gi.is_face=1 and g.id='.$id )->select();
				return $list[0];
			}
			public function setdb($id,$data){
				$res=$this->where('id='.$id)->save($data);

				return $res;
			}
	}